package com.orange.core.service.config;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.orange.core.mapper.config.ConfigMapper;
import com.orange.core.pojo.entity.config.Config;
import com.orange.core.pojo.enums.EConfigKey;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Optional;

@Service
public class ConfigService extends ServiceImpl<ConfigMapper, Config> {

    public String getValueByKey(String key) {
        Config config = this.lambdaQuery()
                .eq(Config::getKey, key)
                .one();
        if (config != null) {
            return config.getValue();
        }
        return null;
    }

    public Integer getIntValueByKey(String key) {
        String value = getValueByKey(key);
        if (value != null) {
            return Integer.parseInt(value);
        }
        return null;
    }

    public Integer getIntValueByKey(String key, int defaultValue) {
        return Optional.ofNullable(getIntValueByKey(key)).orElse(defaultValue);
    }

    public void setValue(String key, Object value) {
        String existValue = getValueByKey(key);
        if (StrUtil.isBlank(existValue)) {
            this.save(new Config()
                    .setKey(key)
                    .setValue(value.toString()));
        } else {
            this.lambdaUpdate()
                    .set(Config::getValue, value.toString())
                    .eq(Config::getKey, key)
                    .update();
        }
    }

    public int getCreditsRate() {
        return getIntValueByKey(EConfigKey.CREDITS_RATE.name(), 0);
    }

    public void setCreditsRate(int creditsRate) {
        setValue(EConfigKey.CREDITS_RATE.name(), creditsRate);
    }

    public int getFirstRegisterCredit() {
        return getIntValueByKey(EConfigKey.FIRST_REGISTER_CREDIT.name(), 0);
    }

    public void setFirstRegisterCredit(int firstRegisterCredit) {
        setValue(EConfigKey.FIRST_REGISTER_CREDIT.name(), firstRegisterCredit);
    }

    public int getCreditParkTimeRate() {
        return getIntValueByKey(EConfigKey.CREDITS_PARK_TIME_RATE.name(), 0);
    }

    public void setCreditParkTimeRate(int creditParkTimeRate) {
        setValue(EConfigKey.CREDITS_PARK_TIME_RATE.name(), creditParkTimeRate);
    }

    public BigDecimal getCreditParkAmountRate() {
        String value = getValueByKey(EConfigKey.CREDITS_PARK_AMOUNT_RATE.name());
        if (value != null) {
            return new BigDecimal(value);
        }
        return BigDecimal.ZERO;
    }

    public void setCreditParkAmountRate(BigDecimal creditParkAmountRate) {
        setValue(EConfigKey.CREDITS_PARK_AMOUNT_RATE.name(), creditParkAmountRate);
    }

    public int getFreeParkTime() {
        return getIntValueByKey(EConfigKey.FREE_PARK_TIME.name(), 0);
    }

    public void setFreeParkTime(int freeParkTime) {
        setValue(EConfigKey.FREE_PARK_TIME.name(), freeParkTime);
    }
}
